
//
//  ShowWebView.m
//  WKWebView
//
//  Created by MyMac on 15/8/13.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "ShowWebViewController.h"
#import <WebKit/WebKit.h>
@interface ShowWebViewController()<WKNavigationDelegate,WKUIDelegate>
@property(nonatomic,strong) WKWebView*wkWebView;
@end
@implementation ShowWebViewController
-(void)viewDidLoad
{
    self.view.backgroundColor=[UIColor redColor];
    self.wkWebView=[[WKWebView alloc]initWithFrame:self.view.bounds];
    NSURLRequest*request=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
    self.wkWebView.navigationDelegate=self;
    self.wkWebView.UIDelegate=self;
    
    [self.wkWebView loadRequest:request];
    [self.view addSubview:self.wkWebView];
 


}
#pragma -mark WKNavigationDelegate
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{

    NSLog(@"开始加载");

}
-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{

    NSLog(@"内容开始返回");


}
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{

    NSLog(@"页面加载完成");

}
-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{

    NSLog(@"页面加载失败%@",error);

}
-(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{


    NSLog(@"提交加载失败%@",error);

}
-(void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation
{

    NSLog(@"受到页面跳转请求");


}
//-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
//{
//
//    NSLog(@"发送请求前决定是否跳转");
//    
//    
//
//}
//-(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
//{
//
//    NSLog(@"受到相应后决定跳转");
//
//}
#pragma -mark UIDelegate
//创建一个新的webview
//-(WKWebView*)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures

/**
 *  webView界面中弹出警告框时调用
 *
 *  @param webView           实现该代理的webView
 *  @param message           警告框中的内容
 *  @param frame             窗口
 *  @param completionHandler 警告框消失时调用
 */
-(void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)())completionHandler
{

    NSLog(@"警告框内容%@",message);
    completionHandler();
   


}
/**
 *  弹出输入框时调用
 *
 *  @param webView           <#webView description#>
 *  @param prompt            <#prompt description#>
 *  @param defaultText       <#defaultText description#>
 *  @param frame             <#frame description#>
 *  @param completionHandler <#completionHandler description#>
 */
-(void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *))completionHandler
{


    NSLog(@"输入框内容%@",defaultText);


}
/**
 *  确认框弹出时调用
 *
 *  @param webView           <#webView description#>
 *  @param message           <#message description#>
 *  @param frame             <#frame description#>
 *  @param completionHandler <#completionHandler description#>
 */
-(void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler
{

    NSLog(@"确认框内容%@",message);

}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
    NSLog(@"%@", message);
}
@end
